package org.zcl.单例;

import lombok.Cleanup;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @PROJECT_NAME:
 * @DESCRIPTION:
 * @USER: ZhuChengLin
 * @DATE: 2021/11/22 10:49
 */
public class Client {
    public static void main(String[] args) {
        HungrySingleton s1 = HungrySingleton.getInstance();
        HungrySingleton s2 = null;
        try {
            // 将s1序列化到磁盘
            FileOutputStream fos = new FileOutputStream("a.obj");
            @Cleanup
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(s1);

            oos.flush();

            @Cleanup
            FileInputStream fis = new FileInputStream("a.obj");
            ObjectInputStream ois = new ObjectInputStream(fis);
            // 从磁盘反序列化
            s2 = (HungrySingleton) ois.readObject();
            System.out.println(s1 == s2);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

